<?php

namespace app\service;

use app\model\AccountBank;
use app\model\AccountBankModel;
use app\model\BusinessType;
use app\model\NetworksBusiness;
use app\model\NetworksModel;
use app\model\PartnerLogModel;
use app\model\WarehouseModel;
use think\facade\Db;
use app\model\Company;

class NetworksService
{
    public static function lists($param)
    {
        $lists = NetworksModel::lists($param);
        foreach ($lists['networklist'] as $key => $value) {
            $lists['networklist'][$key]['service_status_text'] = NetworksModel::escape_service_status($value['service_status']);
            $business = (new NetworksBusiness())->where('id','in',explode(',',$value['business_id']))->column('name');
            $lists['networklist'][$key]['business_id_text'] = $business?implode(',',$business):'';
            $lists['networklist'][$key]['business_type_text'] = (new BusinessType())->find($value['business_type'])->name;
            $lists['networklist'][$key]['company_name'] = Company::where('id',$value['company_id'])->value('name');
            $bank = AccountBank::where(['user_type'=>4,'user_id'=>$value['id'],'status'=>1 ])->find();
            $lists['networklist'][$key]['bank_name'] = $bank?$bank->bank_name:'';
            $lists['networklist'][$key]['bank_branch'] = $bank?$bank->bank_branch:'';
            $lists['networklist'][$key]['bank_no'] = $bank?$bank->bank_no:'';
        }
        return $lists;
    }

    public static function line_data($param)
    {
        return NetworksModel::line_data($param);
    }

    public static function info()
    {
        $warehouseType = WarehouseModel::GetAllWarehouseType();
        $bussineType = WarehouseModel::GetAllBussineType();
        return [
            'warehouseType' => $warehouseType,
            'bussineType' => $bussineType
        ];
    }

    public static function add($data, $logParam)
    {
//        $gpsInfo = baidutogps($data['longitude'], $data['dimension']);
//        if ($gpsInfo != 400) {
//            $data['bd_gps_longitude'] = $gpsInfo[0]['x'];
//            $data['bd_gps_dimension'] = $gpsInfo[0]['y'];
//        }
        Db::startTrans();
        $data['add_time'] = getNowDateTime();
        $data['update_time'] = getNowDateTime();
        $res = NetworksModel::add($data);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '添加网点',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function edit($data, $logParam, $id)
    {
        Db::startTrans();
        $data['update_time'] = getNowDateTime();
        $res = NetworksModel::edit($data, $id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '修改网点信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function del($data, $logParam, $id)
    {
        Db::startTrans();
        $res = NetworksModel::del($id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '删除网点信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }
}